//
//  MyGroupViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/8.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "MyGroupViewController.h"
#import "AddPeopleAndGroupViewController.h"
#import "MyGroupModel.h"
#import "MyGroupTableViewCell.h"
#import "MyChatViewController.h"
#import "CreateGroupViewController.h"
#import "SearchMyGroupViewController.h"

@interface MyGroupViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myGroupTableView;
@property (nonatomic, strong) NSMutableDictionary *myGroupDic; // 存储小组信息
@property (nonatomic, strong) NSMutableArray *myGroupKeyArray; // 存储小组key

@end

@implementation MyGroupViewController

//- (void)viewWillAppear:(BOOL)animated
//{
//    self.navigationController.navigationBar.hidden = NO;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 建立数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"我的小组";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(add:)];
    rightBarItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 添加
- (void)add:(UIBarButtonItem *)barItem
{
    NSLog(@"添加");
    AddPeopleAndGroupViewController *addPeopleAndGroupVC = [[AddPeopleAndGroupViewController alloc] init];
    addPeopleAndGroupVC.friendOrGroup = @"group";
    [self.navigationController pushViewController:addPeopleAndGroupVC animated:YES];
}

#pragma mark - 建立数据源
- (void)addData
{
    // 模拟数据
    NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脑血管讨论组", @"name", @"199", @"memberCount", nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"羊癫疯讨论组", @"name", @"250", @"memberCount", nil];
    NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"高血压讨论组", @"name", @"380", @"memberCount", nil];
    NSMutableArray *myCreateArray = [NSMutableArray arrayWithObjects:dic1, dic2, dic3, nil];
    
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病1组", @"name", @"199", @"memberCount", nil];
    NSDictionary *dic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病2组", @"name", @"250", @"memberCount", nil];
    NSDictionary *dic6 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病3组", @"name", @"380", @"memberCount", nil];
    NSDictionary *dic7 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病4组", @"name", @"199", @"memberCount", nil];
    NSDictionary *dic8 = [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病5组", @"name", @"250", @"memberCount", nil];
    NSDictionary *dic9= [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病6组", @"name", @"380", @"memberCount", nil];
    NSDictionary *dic10= [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病7组", @"name", @"380", @"memberCount", nil];
    NSDictionary *dic11= [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病8组", @"name", @"380", @"memberCount", nil];
    NSDictionary *dic12= [NSDictionary dictionaryWithObjectsAndKeys:@"心脏病9组", @"name", @"380", @"memberCount", nil];
    NSMutableArray *myJoinArray = [NSMutableArray arrayWithObjects:dic4, dic5, dic6, dic7, dic8, dic9, dic10, dic11, dic12, nil];
    NSMutableDictionary *groupDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:myCreateArray, @"我创建的小组", myJoinArray, @"我加入的小组", nil];
    NSArray *keyArray = [groupDic allKeys];
    
    // 初始化小组字典
    self.myGroupDic = [NSMutableDictionary dictionary];
    
    for (int i = 0; i < keyArray.count; i++) {
        NSString *key = keyArray[i];
        NSArray *tempArray = groupDic[key];
        NSMutableArray *modelArray = [NSMutableArray array];
        for (NSDictionary *dic in tempArray) {
            MyGroupModel *model = [[MyGroupModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [modelArray addObject:model];
        }
        [self.myGroupDic setValue:modelArray forKey:key];
    }
    // 初始化小组key数组
    self.myGroupKeyArray = [NSMutableArray arrayWithObjects:@"我创建的小组", @"我加入的小组", nil];

}

#pragma mark - 布局
- (void)addMySubview
{
    self.myGroupTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    self.myGroupTableView.backgroundColor = [UIColor whiteColor];
    self.myGroupTableView.dataSource = self;
    self.myGroupTableView.delegate = self;
    self.myGroupTableView.bounces = NO;
    [self.view addSubview:_myGroupTableView];
    
    // 构建表头
    UIView *whiteBackGround = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 132 * kAdjustParamater)];
    whiteBackGround.userInteractionEnabled = YES;
    whiteBackGround.backgroundColor = [UIColor whiteColor];
    self.myGroupTableView.tableHeaderView = whiteBackGround;
    
    // 输入框
    UILabel *shuRuLabel = [[UILabel alloc] initWithFrame:CGRectMake((375 - 340) / 2* kAdjustParamater, 15 * kAdjustParamater, 340 * kAdjustParamater, 30 * kAdjustParamater)];
    shuRuLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    shuRuLabel.text = @"请输入小组名字";
    shuRuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    shuRuLabel.textAlignment = NSTextAlignmentCenter;
    shuRuLabel.font = [UIFont systemFontOfSize:k17Big];
    shuRuLabel.layer.cornerRadius = 5 * kAdjustParamater;
    shuRuLabel.layer.masksToBounds = YES;
    shuRuLabel.userInteractionEnabled = YES;
    [whiteBackGround addSubview:shuRuLabel];
    
    // 为输入框添加手势
    UITapGestureRecognizer *shuRuGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shuRu:)];
    [shuRuLabel addGestureRecognizer:shuRuGes];
    
    // 输入框底部灰色条
    UILabel *grayLineOnShuRuBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, shuRuLabel.bottom + 15 * kAdjustParamater, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnShuRuBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteBackGround addSubview:grayLineOnShuRuBottom];
    
    // 加号背景
    UIView *viewOnAddBottom = [[UIView alloc] initWithFrame:CGRectMake(0, grayLineOnShuRuBottom.bottom, kScreenWidth, 70 * kAdjustParamater)];
    viewOnAddBottom.backgroundColor = [UIColor whiteColor];
    whiteBackGround.userInteractionEnabled = YES;
    [whiteBackGround addSubview:viewOnAddBottom];
    
    // 添加手势
    UITapGestureRecognizer *createGroupGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createGroup:)];
    [viewOnAddBottom addGestureRecognizer:createGroupGes];
    
    // 加号
    UIImageView *addImageV = [[UIImageView alloc] initWithFrame:CGRectMake(25 * kAdjustParamater, 15 * kAdjustParamater, 40 * kAdjustParamater, 40 * kAdjustParamater)];
    addImageV.image = [UIImage imageNamed:@"huisejiaohao-myGroup"];
   // addImageV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [viewOnAddBottom addSubview:addImageV];
    
    // 创建小组
    UILabel *createGroupLabel = [[UILabel alloc] initWithFrame:CGRectMake(addImageV.right + 20 * kAdjustParamater, 25 * kAdjustParamater, 100 * kAdjustParamater, 20 * kAdjustParamater)];
  //  createGroupLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    createGroupLabel.text = @"创建小组";
    createGroupLabel.textColor = [UIColor blackColor];
    createGroupLabel.textAlignment = NSTextAlignmentLeft;
    createGroupLabel.font = [UIFont systemFontOfSize:k16Big];
    [viewOnAddBottom addSubview:createGroupLabel];
    
    // 加号下面的灰色条
    UILabel *grayLineOnAddImageVBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, viewOnAddBottom.bottom, kScreenWidth, 1 * kAdjustParamater)];
    grayLineOnAddImageVBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [whiteBackGround addSubview:grayLineOnAddImageVBottom];
    
    
}

#pragma mark - 输入
- (void)shuRu:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"输入小组名字");
    SearchMyGroupViewController *searchMyGroupVC = [[SearchMyGroupViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:searchMyGroupVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

#pragma mark - 创建小组
- (void)createGroup:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"创建小组");
    CreateGroupViewController *createGroupVC = [[CreateGroupViewController alloc] init];
    [self.navigationController pushViewController:createGroupVC animated:YES];
}

// tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = self.myGroupKeyArray[section];
    NSArray *value = self.myGroupDic[key];
    return value.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myGroup";
    MyGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取得model
    NSString *key = self.myGroupKeyArray[indexPath.section];
    NSArray *value = self.myGroupDic[key];
    cell.model = value[indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70 * kAdjustParamater;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 取得表头标题
    NSString *key = self.myGroupKeyArray[section];
    NSArray *value = self.myGroupDic[key];
    UIView *grayBackGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 27 * kAdjustParamater)];
    grayBackGroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
    UILabel *fenQuLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 5 * kAdjustParamater, 150 * kAdjustParamater, 17 * kAdjustParamater)];
 //   fenQuLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    fenQuLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    fenQuLabel.textAlignment = NSTextAlignmentLeft;
    fenQuLabel.font = [UIFont systemFontOfSize:k13Big];
    fenQuLabel.text = [NSString stringWithFormat:@"%@ (%ld)", key, value.count];
    [grayBackGroundView addSubview:fenQuLabel];
    return grayBackGroundView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 27 * kAdjustParamater;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1 * kAdjustParamater;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.1 * kAdjustParamater)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = self.myGroupKeyArray[indexPath.section];
    NSArray *value = self.myGroupDic[key];
    MyGroupModel *tempModel = value[indexPath.row];
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_GROUP;
    myChatVC.title = tempModel.name;
    if ([key isEqualToString:@"我创建的小组"]) {
        myChatVC.createOrJoin = @"create";
    } else if ([key isEqualToString:@"我加入的小组"]) {
        myChatVC.createOrJoin = @"join";
    }
    myChatVC.myGroupModel = tempModel;
    [self.navigationController pushViewController:myChatVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
